package com.TomcatServlet;

import jdk.internal.util.xml.impl.Input;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URLEncoder;
import java.net.UnknownHostException;

/**
 * Created by 41579 on 2017/7/13.
 */
public class ClientSimulation {
    public static void main(String[] args) throws IOException {
//        Socket socket = new Socket("127.0.0.1",8080);
//        OutputStream outputStream = socket.getOutputStream();
//        String url="/";
//        String method = "GET";
//        String protocol="HTTP/1.1";
//        String head = method+" "+url+" "+protocol+"\r\n";
//
//        head+="Host: localhost"+"\r\n";
//        head+="Connection: keep-alive"+"\r\n";
//        head+="Cache-Control: max-age=0"+"\r\n";
//        head+="Upgrade-Insecure-Requests: 1"+"\r\n";
//        head+="User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"+"\r\n";
//        head+="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"+"\r\n";
//        head+="Accept-Encoding: gzip, deflate, br\r\n" +
//                "Accept-Language: zh-CN,zh;q=0.8"+"\r\n";
//
//        outputStream.write(head.getBytes());
//        outputStream.flush();
//        InputStream is = socket.getInputStream();
//        byte[] bytes = new byte[1024];
//        if(is.read(bytes)>0){
//            System.out.println(new String(bytes));
//        }


        Socket socket;
        try {
            String data = URLEncoder.encode("k", "UTF-8") + "=" +
                    URLEncoder.encode("abc", "UTF-8");
            InetAddress addr = InetAddress.getByName("127.0.0.1");
            socket = new Socket(addr,8084);
            BufferedWriter wr = new BufferedWriter(new
                    OutputStreamWriter(socket.getOutputStream(), "UTF8"));
            InputStream ins = socket.getInputStream();
            StringBuffer sb = new StringBuffer();
            sb.append("GET /SpringMVC/annotation/test0 HTTP/1.1\r\n");// 注意\r\n为回车换行
            sb.append("Accept-Language: zh-cn\r\n");
            sb.append("Connection: Keep-Alive\r\n");
            sb.append("Host:localhost\r\n");
            sb.append("Content-Length:11\r\n");
            sb.append("Content-Type: application/x-www-form-urlencoded\r\n");
//            sb.append("\r\n");
//            sb.append(data);
            // 接收Web服务器返回HTTP响应包
            wr.write(sb.toString());
            wr.flush();
            BufferedReader rd = new BufferedReader(new
                    InputStreamReader(socket.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                System.out.println(line);
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
